/*
 *    Copyright 2022 CManLH
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.lifeonwalden.ms.common.logger;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.lifeonwalden.ms.common.date.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.Date;

public class LoggerUtil {
    public static final ObjectMapper mapper = new ObjectMapper();

    static {
        SimpleModule module = new SimpleModule("module");
        module.addSerializer(Date.class, new StdSerializer<Date>(Date.class) {
            private static final long serialVersionUID = -7117834188943863569L;

            @Override
            public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                gen.writeString(DateUtil.formatDate(value, DateUtil.VIEW_DHMS));
            }
        });
        mapper.registerModule(module);
    }

    public static <T> Logger getLogger(Class<T> clazz) {
        Logger logger = LogManager.getLogger(clazz);
        return logger;
    }

    public static void info(Logger logger, String methodName, Object... params) {
        if (null != params && params.length > 0) {
            logger.info(methodName + " : {} ", convertValueToString(logger, params));
        } else {
            logger.info(methodName + " : {}");
        }
    }

    public static void error(Logger logger, String methodName, Object... params) {
        if (null != params && params.length > 0) {
            logger.error(methodName + " : {} ", convertValueToString(logger, params));
        } else {
            logger.error(methodName + " : {}");
        }
    }

    public static void debug(Logger logger, String methodName, Object... params) {
        if (null != params && params.length > 0) {
            logger.debug(methodName + " : {} ", convertValueToString(logger, params));
        } else {
            logger.debug(methodName + " : {}");
        }
    }

    public static String convertValueToString(Logger logger, Object... params) {
        try {
            return mapper.writeValueAsString(params);
        } catch (JsonProcessingException e) {
            logger.error(e);

            return "";
        }
    }
}
